<?php

namespace App\Http\Controllers\Api;

use App\Models\Activity;
use App\Models\ActivityUsers;
use App\Models\Integral;
use App\Models\IntegralUsers;
use App\Models\Users;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ActivityController extends Controller
{
    public function index(Request $request)
    {
        $status = $request->input('status');
        $time = $request->input('time');
        $model = Activity::query();
        $model = $model->select('id', 'title', 'thumb', 'stime', 'etime', 'people', 'status', 'created_at');

        if ($time) {
            $model = $model->whereDate('stime', '<=', $time);
            $model = $model->whereDate('etime', '>=', $time);
        } else {
            if ($status == 1) {
                $model = $model->whereDate('etime', '>', Carbon::today());
            } else {
                $model = $model->whereDate('etime', '<', Carbon::today());
            }
        }
        $model = $model->where('status', 1);
        $model = $model->orderBy('stime', 'asc');
        $re = $model->orderBy('created_at', 'desc')->paginate(10);
        return showMsg('获取成功', 200, $re);
    }

    public function getDateMark(Request $request)
    {
        $year = $request->input('year');
        $month = $request->input('month');
        $days = 0;
        if ($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12) {
            $days = 31;
        } elseif ($month == 4 || $month == 6 || $month == 9 || $month == 11) {
            $days = 30;
        } else {
            // 判断是否为闰年
            if (checkdate(2, 29, $year)) {
                $days = 29;
            } else {
                $days = 28;
            }
        }
        $result = [];
        for ($i = 0; $i < $days; $i++) {
            $time = $year . '-' . $month . "-" . ($i + 1);
            $re = Activity::whereDate('stime', '<=', $time)->whereDate('etime', '>=', $time)->where('status', 1)->first();
            if ($re) {
                $result[$i]['date'] = $time;
                $result[$i]['title'] = "活动";
            }
        }
        return showMsg('获取成功', 200, $result);
    }

    //获取活动详情
    public function details(Request $request)
    {
        $id = $request->get('id');
        $res = Activity::where('id', $id)->first();
        //是否报名
        $bm = ActivityUsers::where(['activity_id' => $id, 'uid' => $this->uId])->first();
        $res['bm'] = 0;
        if ($bm) {
            $res['bm'] = 1;
        }
        return showMsg('获取成功', 200, $res);
    }

    //参与活动
    public function setActivity(Request $request)
    {
        $mobile = $request->get('mobile', "");
        $nickname = $request->get('nickname', "");
        $company = $request->get('company', "");
        $id = $request->get('activity_id', 0);

        $activity = Activity::where('id', $id)->first();
        if (empty($activity)) {
            return showMsg('活动不存在！', 201);
        }
        //判断是否兑换过
        $where['activity_id'] = $id;
        $where['uid'] = $this->uId;
        $re = ActivityUsers::where($where)->first();
        if ($activity['etime'] && strtotime($activity['etime']) < time()) {
            return showMsg('活动已结束！', 201);
        }
        if ($re) {
            return showMsg('您已参与，请勿重复提交！', 201);
        }
        if ($activity['people'] != 0 && $activity['people'] <= $activity['user_nums']) {
            return showMsg('参与人数已满，请选择其他活动！', 201);
        }
        //添加申请记录
        $inte['uid'] = $this->uId;
        $inte['activity_id'] = $id;
        $inte['mobile'] = $mobile;
        $inte['nickname'] = $nickname;
        $inte['company'] = $company;
        ActivityUsers::create($inte);
        return showMsg('兑换成功！', 200);
    }

    //取消活动
    public function setActivityCancel(Request $request)
    {
        $id = $request->get('id');
        ActivityUsers::where('id', $id)->update(['status' => 2]);
        return showMsg('操作成功', 200);
    }

    public function setActivitySuccess(Request $request)
    {
        $user_id = $this->uId;
        $integral = Integral::where('id', 2)->first();
        $nums = IntegralUsers::where(['user_id' => $user_id, 'integral_id' => 2])->whereDate('created_at', today())->count();
        if ($nums < $integral['total']) {
            $data['user_id'] = $user_id;
            $data['integral_id'] = 2;
            $data['integral'] = $integral['integral'];
            $data['status'] = 1;
            IntegralUsers::create($data);
            Users::where('id', $user_id)->increment('integral', $integral['integral']);
        }
        return showMsg('操作成功', 200);
    }

    //我的活动
    public function myActivity(Request $request)
    {
        $type = $request->get('type', 0);
        $model = ActivityUsers::query();
        $model = $model->with('activity');
        if ($type == 1) {
            $model = $model->whereHas('activity', function ($query) use ($type) {
                $query->whereDate('etime', '>=', date('Y-m-d', time()));
                $query->whereDate('stime', '<=', date('Y-m-d', time()));
            });
        }
        if ($type == 2) {
            $model = $model->whereHas('activity', function ($query) use ($type) {
                $query->whereDate('etime', '<', date('Y-m-d', time()));
            });
            $model = $model->where('status', '!=', 2);
        }
        if ($type == 3) {
            $model = $model->where('status', 2);
        }
        $model = $model->where('uid', $this->uId);
        $model = $model->orderBy('created_at', 'desc');
        $course = $model->paginate(10);
        foreach ($course as $value) {
            $value['times'] = date('m-d', strtotime($value['activity']['stime']));
            $value['week'] = get_week_date(strtotime($value['activity']['stime']));
            if ($value['status'] != 2) {
                if (strtotime($value['activity']['stime']) >= time() && time() <= strtotime($value['activity']['etime'])) {
                    $value['status'] = 0;
                } elseif (strtotime($value['activity']['etime']) < time()) {
                    $value['status'] = 1;
                }
            }
        }
        return showMsg('获取成功！', 200, $course);
    }

}
